package com.document.controll;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.Cnd;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.document.model.Org;
import com.document.model.PageModel;
import com.document.model.Role;
import com.document.model.User;
import com.document.utils.SystemContext;

@IocBean
@InjectName
public class UserAction extends BaseAction {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	@Ok("jsp:pages.user.userList")
	@At("/user/listUser")
	public void list(@Param("currentPage") int currentPage,@Param("orgCode") String orgCode,
			HttpServletRequest req,ServletContext context) {
		List<User> list = null;
		int count = basicDao.searchCount(User.class,Cnd.where("orgcode", "like", orgCode + "-%"));	
		if(orgCode != null && !"".equals(orgCode)){			
			 list = basicDao.searchByPage(User.class, Cnd.where("orgcode", "=", orgCode), 
					currentPage, SystemContext.PAGE_SIZE);	
		}else{
			list = basicDao.searchByPage(User.class, null, 
					currentPage, SystemContext.PAGE_SIZE);
		}
		int len = list.size();
		if(len > 0 && len <= SystemContext.PAGE_SIZE){
			for(int i = list.size();i < SystemContext.PAGE_SIZE;i++){
				list.add(new User());
			}
		}	
		int maxPage = basicDao.maxPageSize(count, SystemContext.PAGE_SIZE);	
		PageModel<User> pm = new PageModel<User>(list, maxPage);
		req.setAttribute("navid", 0);
		req.setAttribute("pm", pm);
		req.setAttribute("orgCode", orgCode);
	}
	
	@At("/user/userExists")
	public void userExists(@Param("user.userName") String userName,HttpServletResponse resp){
		User user = basicDao.findByCondition(User.class, Cnd.where("username", "=", userName));
		String rvt = "{exists:true}";
		if(user == null){
			rvt = "{exists:false}";
		}
		super.write(resp, rvt);
	}
	
	@At("/user/add")
	public void addUser(@Param("::user.") User user,HttpServletResponse resp){
		user.setCreateTime(sdf.format(new Date()));	
		user.setUserStatus("1");
		user = basicDao.save(user);
		String rvt = "{success:false}";
		if(user != null && user.getId() > 0){
			rvt = "{success:true}";
		}
		write(resp,rvt);
	}
	
	@At("/user/update")
	public void updateOrg(@Param("::user.") User user,HttpServletResponse resp){		
		User _user = basicDao.find(user.getId(), User.class);
		_user.setFullName(user.getFullName());
		_user.setSex(user.getSex());
		_user.setPhone(user.getPhone());
		_user.setMobPhone(user.getMobPhone());
		_user.setQq(user.getQq());
		_user.setAddress(user.getAddress());
		_user.setRemark(user.getRemark());
		_user.setUserType(user.getUserType());
		boolean success = basicDao.update(_user);
		String rvt = "{success:" + success + "}";
		write(resp,rvt);
	}
	
	@At("/user/findUserById")
	public void findUserById(@Param("userId") int userId,HttpServletResponse resp){
		User user = basicDao.find(userId, User.class);
		write(resp,Json.toJson(user,JsonFormat.full()));
	}
	
	@At("/user/delete")
	public void deleteOrg(@Param("userId") int userId,HttpServletResponse resp){
		boolean success = basicDao.delById(userId, User.class);			
		write(resp,"{success:" + success + "}");
	}
	
	@At("/user/batchDelete")
	public void batchDeleteOrg(@Param("userIds") String userIds,HttpServletResponse resp){
		boolean success = false;
		try{
			basicDao.deleteByIds(User.class, userIds);	
			success = true;
		}catch(Exception e){}
		write(resp,"{success:" + success + "}");
	}
	
	@At("/user/resetPwd")
	public void resetPwd(@Param("userId") int userId,@Param("pwd") String pwd,HttpServletResponse resp){
		boolean success = false;		
		User user = basicDao.find(userId, User.class);
		if(user != null){
			user.setPwd(pwd);
			success = basicDao.update(user);		
		}
		write(resp,"{success:" + success + "}");
	}
	
	@At("/user/userType")
	public void getUserType(HttpServletResponse resp,ServletContext context){
		List roleList = (List) context.getAttribute("roleList");
		super.write(resp, Json.toJson(roleList, JsonFormat.full()));
	}
}
